package com.bree.erp.jsf.view;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;

import com.bree.erp.entity.ReturnsBalance;

@ManagedBean(name = "dtReserveBalanceLazyView")
@ViewScoped
public class ReserveBalanceLazyView extends AbstractBalanceLazyView {

	private static final long serialVersionUID = 1L;
	
	private static Logger LOG = Logger.getLogger(ReserveBalanceLazyView.class);

	@ManagedProperty(value="#{dtReturnWithdrawalView}")
	private ReturnWithdrawalView returnWithdrawalView;
	
	private ReturnsBalance selectedItem;
	
	private double quantity;
    
    public void onRowSelect(SelectEvent event) {
    	
    	String msgStr = "";
    	if(event.getObject() == null) {
    		LOG.error("Event object is NULL!!!!s " + event.getSource());
    	
    	}
    	
    	
    	msgStr = ((ReturnsBalance) event.getObject()).getId() + "";
        FacesMessage msg = new FacesMessage("Item Selected", msgStr);
        FacesContext.getCurrentInstance().addMessage(null, msg);
        
        LOG.debug(msgStr);
        
        quantity = 1;
    }
    
    public void reserve() {
    	
    	LOG.debug("Reserving " + this.quantity + " " + this.selectedItem.getPartUnit().getShortUnit());
    	
    	returnWithdrawalView.reserve(selectedItem, quantity);
    }


	/**
	 * @return the selectedItem
	 */
	public ReturnsBalance getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(ReturnsBalance selectedItem) {
		this.selectedItem = selectedItem;
	}

	/**
	 * @return the quantity
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity the quantity to set
	 */
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the returnWithdrawalView
	 */
	public ReturnWithdrawalView getReturnWithdrawalView() {
		return returnWithdrawalView;
	}

	/**
	 * @param returnWithdrawalView the returnWithdrawalView to set
	 */
	public void setReturnWithdrawalView(ReturnWithdrawalView returnWithdrawalView) {
		this.returnWithdrawalView = returnWithdrawalView;
	}
}
